package com.soap.desensitize.strategy.irreversible;

import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;
import com.soap.desensitize.exception.strategy.EncodeException;
import org.apache.log4j.Logger;

import java.security.NoSuchAlgorithmException;

/**
 * @author qgp
 * @desc 身份证替换策略
 * @time 2022-04-20
 */
public class IdCardStrategy extends AbstractIRreversibleEncry<String> {
    private static IdCardStrategy instance = null;
    public static Logger log = Logger.getLogger(IdCardStrategy.class);
    private IdCardStrategy() {
    }

    /**
     * @return
     * @desc 获得实例
     */
    public static synchronized IdCardStrategy getInstance() {
        if (instance == null) {
            instance = new IdCardStrategy();
        }
        return instance;
    }

    /**
     * @desc   身份证加密
     * @param  data
     * @return
     * @throws NoSuchAlgorithmException
     * @throws EncodeException
     */
    @Override
    public String encode(String data) throws EncodeException {
        boolean idNumber = checkIdCard(data);
        log.info("身份证验证" + idNumber);
        String encodeIdCard = "";
        if (idNumber) {
            encodeIdCard = data.substring(0, 7) + "****" + data.substring(11, data.length());
            log.info("加密后的身份证号码：" + encodeIdCard);
            return encodeIdCard;
        }
        throw new EncodeException("身份证不合法，无法进行数据脱敏。身份证：" + data);
    }

    /**
     * @desc   身份证替换策略
     * @param IDNumber
     * @return
     */
    public static boolean checkIdCard(String IDNumber) {
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

        boolean matches = IDNumber.matches(regularExpression);//用于检测字符串是否匹配给定的正则表达式
        //判断第18位校验值
//        if (matches) {
//            if (IDNumber.length() == 18) {
//                try {
//                    char[] charArray = IDNumber.toCharArray();
//                    //前十七位加权因子
//                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
//                    //这是除以11后，可能产生的11位余数对应的验证码
//                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
//                    int sum = 0;
//                    for (int i = 0; i < idCardWi.length; i++) {
//                        int current = Integer.parseInt(String.valueOf(charArray[i]));
//                        int count = current * idCardWi[i];
//                        sum += count;
//                    }
//                    char idCardLast = charArray[17];
//                    int idCardMod = sum % 11;
//                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
//                        return true;
//                    } else {
//                        return false;
//                    }
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    return false;
//                }
//            }
//        }
        return matches;
    }
}
